package cn.liukuan.jps.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created by liukuan on 16/5/29.
 */
public class PropertiesUtils {

    private Properties prop;

    private PropertiesUtils(){

        prop = new Properties();
        try {
            prop.load(PropertiesUtils.class.getClassLoader().getResourceAsStream("lucene.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class PropertiesHolder {
        public static PropertiesUtils Instance = new PropertiesUtils();
    }

    public static PropertiesUtils getInstance(){
        return PropertiesHolder.Instance;
    }

    public static String getProp(String key){

        return getInstance().prop.getProperty(key);

    }

    public static Integer getIntegerPropOrElse(String key, int value){

        Integer result = 0;

        String property = getInstance().prop.getProperty(key);

        try{
            result = Integer.parseInt(property);
        }catch (Exception e){
            result = value;
        }

        return result;
    }

}
